// miniprogram/pages/shiyong/add/add.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    user: '请授权'
  },
  onLoad: function () {
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        console.log('login返回的', res.result)
        this.setData({
          openid: res.result.openid
        })
      }
    })
  },
  onbutton: function () {
    wx.getUserInfo({
      success: res => {
        // app.globalData.userInfo = res.userInfo
        console.log(res.userInfo)
        this.setData({
          userInfo: res.userInfo,
          user: res.userInfo.nickName
        })
      }
    })
  },

  submit(e) {
    let a = e.detail.value
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (this.data.userInfo === null) {
      console.log('请先授权')
      wx.showLoading({
        title: '请先授权',
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)

    } else {
      var userInfo = this.data.userInfo
   var openid = this.data.openid
      wx.cloud.callFunction({
        name: 'add',
        data: {
          userInfo,
          a,
          openid
        },
        complete: res => {
          console.log('callFunction test result: ', res)
        }
      })
      wx.navigateBack({
        delta: 1
      })
    }
  }
})